package dyl.sys.tag;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;

import dyl.common.util.JdbcTemplateUtil;
import dyl.common.util.SpringContextUtil;
import dyl.sys.bean.SysConfigInfo;
import dyl.sys.init.AuthCache;

/**
 * <DL>
 * <DT><B> 类名称: </B>选择列表初始化标签 </DT><br>
 * <p>
 * </DL>
 * <p>
 * @author Dyl
 * @version 1.00, 2012-7-26
 */
public class SelectTag extends TagSupport {
	private static final long serialVersionUID = 8125593281113591128L;

	private String name;
	public void setName(String name){
		this.name = name;
	}
	private String layFilter;
	public void setLayFilter(String layFilter) {
		this.layFilter = layFilter;
	}
	private String selectId;
	public void setSelectId(String selectId) {
		this.selectId = selectId;
	}
	private Integer type;
	public void setType(Integer type) {
		this.type = type;
	}
	private String configType;
	public void setConfigType(String configType) {
		this.configType = configType;
	}
	private Boolean isRequired =false;
	public void setIsRequired(Boolean isRequired) {
		this.isRequired = isRequired;
	}
	private Boolean isCache = true ;
	public void setIsCache(Boolean isCache) {
		this.isCache = isCache;
	}
	private String yxdm;
	public void setYxdm(String yxdm) {
		this.yxdm = yxdm;
	}
	private Boolean hasEmptyOp = true ;
	public void setHasEmptyOp(Boolean hasEmptyOp) {
		this.hasEmptyOp = hasEmptyOp;
	}
	JdbcTemplateUtil jdbcTemplate  =  (JdbcTemplateUtil)SpringContextUtil.getApplicationContext().getBean("jdbcTemplate");
	Object[] obj = new Object[]{};
	public int doStartTag() throws JspException {
		Writer out = pageContext.getOut();
		String str = "<select lay-search='' class='layui-select' ";
		try{
			if(isRequired){
				str+="lay-verify='required'";
			}
			if(StringUtils.isNotEmpty(name)){
				str+=" name='"+name+"' ";
			}
			if(StringUtils.isNotEmpty(layFilter)){
				str+=" lay-filter='"+layFilter+"' ";
			}
			str+=" >";
			if(hasEmptyOp)
			str+="<option value=''></option>";
			
			if(type!=null){//sql语句
				List<Map<String,Object>> list = null;
				if(isCache){
					String key  = type.toString();
					if(yxdm!=null){
						key=type+"_"+yxdm;
					}
					if(AuthCache.selectDataMap.get(key)!=null&&AuthCache.selectDataMap.get(key).size()>0){
						list = AuthCache.selectDataMap.get(key);
					}else{
						List<Object> para = new ArrayList<Object>();
						String sql = (String)obj[type];
						if(yxdm!=null){
							sql+=" where yxdm = ?";
							para.add(yxdm);
						}
						list = jdbcTemplate.queryForList(sql,para.toArray());
						AuthCache.selectDataMap.put(key,list);
					}
				}else{
					List<Object> para = new ArrayList<Object>();
					String sql = (String)obj[type];
					if(yxdm!=null){
						sql+=" where yxdm = ?";
						para.add(yxdm);
					}
					list = jdbcTemplate.queryForList(sql,para.toArray());
				}
				for (int i = 0; i < list.size(); i++){
					 int num=0;
					 String key = "";
					 String value = "";
					 for (Entry<String,Object> entry : list.get(i).entrySet()){
						 if(num==0){
							 key=(String)entry.getValue();
							 num++;
						 }else if(num++==1){
							 value=(String)entry.getValue(); 
						 }
				     }
					 str+=setOption(key, value);
				}
			}else if(configType!=null){
				List<SysConfigInfo>  configList =  AuthCache.sysConfigMap.get(configType);
				for (SysConfigInfo c:configList) {
					 str+=setOption(c.getCKey(), c.getCValue());
				}
			}
			str+="</select>";
			out.write(str.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	private String setOption(String key,String value){
		String str = "";
		if(StringUtils.isNotEmpty(selectId)&&selectId.equals(key)){
			 str+="<option value='"+key+"' selected='selected' >"+value+"</option>";
		 }else{
			 str+="<option value='"+key+"'>"+value+"</option>";
		 }
		return str ;
	}
}
